<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetIssuesNumber.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\duobao;

use app\api\model\dao\DuoBao;
use app\api\model\dao\Goods;
use app\api\model\dao\IssuesNumber;
use app\api\model\dao\Lucky;
use app\api\model\dao\UserAccount;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class GetIssuesNumberByNo extends PageBase
{

    private $objIssuesNumber;
    private $objDuobao;
    private $objLucky;
    private $objGoods;
    private $objUserAccount;

    public function __construct()
    {
        parent::__construct();
        $this->objIssuesNumber = new IssuesNumber();
        $this->objDuobao = new DuoBao();
        $this->objLucky = new Lucky();
        $this->objGoods = new Goods();
        $this->objUserAccount = new UserAccount();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'issues_no' => 'require',
        ];
        $msg = [
            'issues_no.require' => '期号ID必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        $user_id = !empty($this->user_id) ? $this->user_id : 0;

        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $issues_no = $param['issues_no'];

        $arrConds = [
            'issues_no' => $issues_no,
        ];
        $ret = $this->objIssuesNumber->getIssuesNumberByCond($arrConds);

        if (!empty($ret)) {
            $user_duobao = ['0' => 0];
            $db_no_arr = [];
            $reward_info = [];
            $user_account = [];
            $user_duobao_arr = [];

            $arrConds = [
                'issues_no' => $issues_no,
            ];
            $arrFields = [];
            $duobaos = $this->objDuobao->getDuoBaoByCond($arrConds, $arrFields);
            if (!empty($duobaos)) {
                foreach ($duobaos as $duobao) {
                    $user_duobao_arr[$duobao['user_id']][] = $duobao;
                    $db_no_arr[$duobao['db_no']] = $duobao;
                    if (!isset($user_duobao[$duobao['user_id']])) {
                        $user_duobao[$duobao['user_id']] = 1;
                    } else {
                        $user_duobao[$duobao['user_id']] += 1;
                    }
                }
            }

            $arrConds = [
                'id' => ['in', implode(',', array_keys($user_duobao))],
            ];
            $arrFields = ['id', 'user_name'];
            $users = $this->objUserAccount->getUserAccountByConds($arrConds, $arrFields);
            if (!empty($users)) {
                foreach ($users as &$user) {
                    $user['user_id'] = $user['id'];
                    $user['buy_numbers'] = $user_duobao[$user['id']];
                    $user_account[$user['id']] = $user;
                }
            }

            $arrConds = [
                'issues_no' => $issues_no,
            ];
            $arrFields = ['db_no'];
            $lucky = $this->objLucky->getLuckyByCond($arrConds, $arrFields);
            if (!empty($lucky)) {
                $db_no = $lucky[0]['db_no'];
                $reward_info['reward_id'] = $db_no_arr[$db_no]['user_id'];
                $reward_info['reward_name'] = $user_account[$db_no_arr[$db_no]['user_id']]['user_name'];
                $reward_info['buy_volume'] = $user_duobao[$db_no_arr[$db_no]['user_id']];
                $reward_info['open_time'] = date('Y年m月d日 H时i分s秒', strtotime($ret[0]['end_time']));
                $reward_info['reward_no'] = $db_no;
            }


            $goods_id = $ret[0]['goods_id'];
            $arrConds = [
                'goods_id' => $goods_id,
            ];
            $arrFields = [];
            $counts = $this->objLucky->getCount($arrConds);
            $ret[0]['no'] = "第" . ($counts + 1) . "期";

            $goods = $this->objGoods->getGoodsByCond(['goods_id' => $goods_id]);
            $ret[0]['goods_name'] = $goods[0]['goods_name'];
            $ret[0]['goods_thumb'] = $goods[0]['goods_thumb'];
            $ret[0]['goods_brief'] = $goods[0]['goods_brief'];
            $ret[0]['goods_img'] = $goods[0]['goods_img'];
            $ret[0]['goods_desc'] = $goods[0]['goods_desc'];
            $ret[0]['remain'] = $ret[0]['volume'] - $ret[0]['current_volume'];
            $ret[0]['sell_percent'] = '' . (round($ret[0]['current_volume'] / $ret[0]['volume'], 2) * 100) . '%';
            $ret[0]['status_desc'] = "第" . ($counts + 1) . "期" . Flag::$map_duobao[$ret[0]['status']];
            $ret[0]['buyer_number'] = 0;

            if ($ret[0]['status'] == 0) {
                $ret[0]['reward_info'] = [
                    'reward_name' => '小强',
                    'reward_id' => '34',
                    'buy_volume' => 100,
                    'open_time' => '2017年03月25日23时20分40秒',
                    'reward_no' => '335555',
                ];
                if (!empty($reward_info)) {
                    $ret[0]['reward_info'] = $reward_info;
                }
            } else {
                $ret[0]['reward_info'] = [];
            }

            $ret[0]['buy_list'] = [
                ['user_name' => '小强', 'user_id' => 34, 'buy_numbers' => 10,],
                ['user_name' => '小明', 'user_id' => 35, 'buy_numbers' => 25,],
            ];
            if (!empty($users)) {
                $ret[0]['buy_list'] = $users;
            }

            $ret[0]['my_duobao_list'] = [];
            if ($user_id) {
                $ret[0]['my_duobao_list'] = !empty($user_duobao_arr[$user_id]) ? $user_duobao_arr[$user_id] : [];
            }
        }

        if ($ret === false) {
            return $this->getRes(Error::ERR_SYS);
        }

        $this->data = $ret[0];

        return $this->getRes();
    }
}
